﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Domain.MainModule.Entities;
using Infraestructure.Data.MainModule.UnitOfWork;
using Infraestructure.CrossCutting.Logging;
using Domain.MainModule.Identities;
using Infraestructure.Data.MainModule.Repositories;
using System.Collections;

namespace Infraestructure.Data.MainModule.Tests.RepositoriesTests
{
    [TestClass()]
    public class IdentityRepositoryTest : RepositoryTestsBase<Identity>
    {
        //Guid identityId = new Guid(TestConstants.IDENTITY_ID);
        //Guid identityUserId = new Guid(TestConstants.USER_ID);

        public override System.Linq.Expressions.Expression<Func<Identity, bool>> FilterExpression
        {
            get { return a => a.IdentityId == TestConstants.Instance.IDENTITY_ID; }
        }

        public override System.Linq.Expressions.Expression<Func<Identity, Guid>> OrderByExpression
        {
            get { return a => a.IdentityId; }
        }

        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException))]
        public void FindIdentity_Invoke_NullSpecThrowNewArgumentNullException_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IIdentityRepository repository = new IdentityRepository(context, traceManager);

            //Act
            repository.FindIdentity(null);
        }

        #region IdentityIdSpecification

        [TestMethod()]
        public void FindIdentity_Invoke_InvalidIdentityIdReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IIdentityRepository repository = new IdentityRepository(context, traceManager);

            Guid invalidIdentityId = new Guid("11111111-1111-1111-1111-111111111111");

            IdentityIdSpecification spec = new IdentityIdSpecification(invalidIdentityId);

            //Act
            IEnumerable<Identity> identities = repository.FindIdentity(spec);

            //Assert
            Assert.IsNull(identities);
        }

        [TestMethod()]
        public void FindIdentityByIdentityId_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IIdentityRepository repository = new IdentityRepository(context, traceManager);

            IdentityIdSpecification spec = new IdentityIdSpecification(TestConstants.Instance.IDENTITY_ID);

            //Act
            IEnumerable<Identity> identities = repository.FindIdentity(spec);
            //Assert
            Assert.IsNotNull(identities.First());
        }

        #endregion

        #region IdentityUserIdSpecification

        [TestMethod()]
        public void FindIdentity_Invoke_InvalidIdentityUserIdReturnNullObject_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IIdentityRepository repository = new IdentityRepository(context, traceManager);

            Guid invalidIdentityUserId = new Guid("11111111-1111-1111-1111-111111111111");

            IdentityUserIdSpecification spec = new IdentityUserIdSpecification(invalidIdentityUserId);

            //Act
            IEnumerable<Identity> identities = repository.FindIdentity(spec);

            //Assert
            Assert.IsNull(identities);
        }

        [TestMethod()]
        public void FindIdentityByIdentityUserId_Invoke_Test()
        {
            //Arrange 
            IMainModuleUnitOfWork context = GetUnitOfWork();
            ITraceManager traceManager = this.GetTraceManager();
            IIdentityRepository repository = new IdentityRepository(context, traceManager);

            IdentityUserIdSpecification spec = new IdentityUserIdSpecification(TestConstants.Instance.USER_ID);

            //Act
            IEnumerable<Identity> identities = repository.FindIdentity(spec);

            //Assert
            Assert.IsNotNull(identities);
        }

        #endregion
    }
}
